package com.uow.algdemo.material;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.RectF;
import android.widget.ImageView;

public class Point extends ImageView
{
	/**
     * Paint for background color.
     */
	private final Paint mPaint = new Paint();
	/**
     * Paint for font color.
     */
	private Paint tPaint = new Paint();
	/**
     * Value for integer.
     */
	private int iValue;
	/**
     * Value for String.
     */
	private String sValue;
	/**
     * Value for background color, default blue.
     */
	private int bgColor = Color.BLUE;
	
	/**
     * @param context The Context the view is running in, through which it can access the current theme, resources, etc.
	 */
	public Point(Context context)
	{
		super(context);
		mPaint.setColor(bgColor);
		mPaint.setAntiAlias(true);
	}

	/**
     * @param context The Context the view is running in, through which it can access the current theme, resources, etc.
	 * @param nValue Integer value for point.
     * @param size Size of point on screen in pixel.
	 */
	public Point(Context context, int nValue, int size)
	{
		super(context);
		mPaint.setColor(bgColor);
		this.iValue = nValue;
		mPaint.setAntiAlias(true);
		tPaint.setTextSize(size - 15);
	}
	
	/**
     * @param context The Context the view is running in, through which it can access the current theme, resources, etc.
	 * @param sValue String value for point.
     * @param size Size of point on screen in pixel.
	 */
	public Point(Context context, String sValue, int size)
	{
		super(context);
		mPaint.setColor(bgColor);
		this.sValue = sValue;
		mPaint.setAntiAlias(true);
		tPaint.setTextSize(size - 15);
	}
	/**
	 * @param canvas the canvas on which the background will be drawn.
	 */
	public void onDraw(Canvas canvas)
	{ 
		super.onDraw(canvas);
		int height = this.getHeight();
		int width = this.getWidth();
		RectF rect = new RectF(0,0,width,height);
		canvas.drawRoundRect(rect, 5, 5, mPaint);
		tPaint.setColor(Color.WHITE); 
		tPaint.setTextAlign(Align.CENTER); 

		FontMetrics fontMetrics = tPaint.getFontMetrics(); 
		float fontHeight = fontMetrics.bottom - fontMetrics.top; 
		float textBaseY = height - (height - fontHeight) / 2 - fontMetrics.bottom; 
		if(sValue != null)
		{
			canvas.drawText(sValue, width / 2, textBaseY, tPaint);
		}
		else
		{
			canvas.drawText(String.valueOf(iValue), width / 2, textBaseY, tPaint);
		}
	}
	/**
     * This method set color of background to green.
	 */
	public void colorGreen()
	{
		bgColor = Color.GREEN;
		mPaint.setColor(bgColor);
		this.invalidate();
	}
	/**
     * This method set color of background to blue.
	 */
	public void colorBlue()
	{
		bgColor = Color.BLUE;
		mPaint.setColor(bgColor);
		this.invalidate();
	}
	/**
     * This method set color of background to red.
	 */
	public void colorRed()
	{
		bgColor = Color.RED;
		this.invalidate();
		mPaint.setColor(bgColor);
	}
}
